{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "标准化矩阵如下：\n",
      "           X1正       X2正       X3正       X4正       X5正       X6正       X7正  \\\n",
      "年份                                                                           \n",
      "2008  0.000000  0.000000  0.000000  0.000000  0.846154  0.000000  0.000000   \n",
      "2009  0.065426  0.039530  0.144290  0.089431  0.892308  0.064516  0.150185   \n",
      "2010  0.177982  0.160256  0.294092  0.186992  0.969231  0.209677  0.295330   \n",
      "2011  0.322051  0.236111  0.477403  0.300813  1.000000  0.198925  0.474129   \n",
      "2012  0.417322  0.302350  0.701389  0.398374  0.784615  0.290323  0.549444   \n",
      "2013  0.513302  0.364316  0.876323  0.487805  0.730769  0.247312  0.669385   \n",
      "2014  0.603953  0.438034  0.936618  0.577236  0.946154  0.268817  0.724833   \n",
      "2015  0.684472  0.606838  1.000000  0.707317  0.907692  0.354839  0.902001   \n",
      "2016  0.774822  0.771368  0.933532  0.861789  0.000000  0.768817  0.931801   \n",
      "2017  0.878677  0.882479  0.448964  0.918699  0.715385  0.844086  0.964566   \n",
      "2018  1.000000  1.000000  0.269290  1.000000  0.746154  1.000000  1.000000   \n",
      "\n",
      "           X8正       X9正   X10负      X11负      X12负      X13负  \n",
      "年份                                                             \n",
      "2008  0.176190  0.000000  0.050  0.444444  0.507317  0.845771  \n",
      "2009  0.000000  0.053030  0.000  1.000000  0.409756  0.014925  \n",
      "2010  0.128571  0.143939  0.150  0.694444  0.253659  0.000000  \n",
      "2011  0.319048  0.196970  0.225  0.611111  0.253659  0.104478  \n",
      "2012  0.442857  0.242424  0.325  0.666667  0.000000  0.278607  \n",
      "2013  0.580952  0.257576  0.325  0.527778  0.263415  0.432836  \n",
      "2014  0.700000  0.363636  0.425  0.555556  0.970732  0.213930  \n",
      "2015  0.795238  0.431818  0.500  0.472222  1.000000  1.000000  \n",
      "2016  0.900000  0.575758  0.575  0.000000  0.990244  0.766169  \n",
      "2017  1.000000  0.583333  0.800  0.138889  0.956098  0.691542  \n",
      "2018  0.880952  1.000000  1.000  0.638889  0.975610  0.577114  \n",
      "========================================================================\n",
      "各指标的权重为\n",
      "X1正     0.0777371\n",
      "X2正     0.0953194\n",
      "X3正     0.0700443\n",
      "X4正     0.0780834\n",
      "X5正     0.0308684\n",
      "X6正      0.102911\n",
      "X7正     0.0598037\n",
      "X8正     0.0723596\n",
      "X9正     0.0967402\n",
      "X10负     0.091658\n",
      "X11负    0.0508249\n",
      "X12负    0.0697605\n",
      "X13负     0.103889\n",
      "Name: 指标权重, dtype: object\n",
      "========================================================================\n",
      "--------最优解-------\n",
      "年份\n",
      "2008       0.2511\n",
      "2009     0.261473\n",
      "2010     0.236657\n",
      "2011     0.213192\n",
      "2012     0.191877\n",
      "2013     0.172718\n",
      "2014     0.158381\n",
      "2015     0.114191\n",
      "2016    0.0942496\n",
      "2017     0.083643\n",
      "2018    0.0708878\n",
      "Name: 最优解, dtype: object\n",
      "--------end-------\n",
      "========================================================================\n",
      "--------最劣解-------\n",
      "年份\n",
      "2008      0.10173\n",
      "2009    0.0671073\n",
      "2010    0.0690168\n",
      "2011    0.0877276\n",
      "2012     0.110785\n",
      "2013     0.130883\n",
      "2014     0.158045\n",
      "2015     0.206286\n",
      "2016     0.220072\n",
      "2017     0.229552\n",
      "2018     0.259584\n",
      "Name: 最劣解, dtype: object\n",
      "--------end-------\n",
      "========================================================================\n",
      "--------综合评价值-------\n",
      "年份\n",
      "2008    0.288326\n",
      "2009    0.204234\n",
      "2010    0.225786\n",
      "2011    0.291531\n",
      "2012    0.366035\n",
      "2013    0.431103\n",
      "2014    0.499468\n",
      "2015    0.643684\n",
      "2016     0.70015\n",
      "2017    0.732937\n",
      "2018    0.785495\n",
      "Name: 综合评价值, dtype: object\n",
      "--------end-------\n",
      "========================================================================\n",
      "--------综合排名-------\n",
      "年份\n",
      "2018    0.785495\n",
      "2017    0.732937\n",
      "2016     0.70015\n",
      "2015    0.643684\n",
      "2014    0.499468\n",
      "2013    0.431103\n",
      "2012    0.366035\n",
      "2011    0.291531\n",
      "2010    0.225786\n",
      "2009    0.204234\n",
      "2008    0.288326\n",
      "Name: 综合评价值, dtype: object\n",
      "========================================================================\n",
      "--------最终排名-------\n",
      "排名第 1 名 2009 分数为 0.2042341813186129\n",
      "排名第 2 名 2010 分数为 0.22578600802505974\n",
      "排名第 3 名 2011 分数为 0.29153125234354516\n",
      "排名第 4 名 2012 分数为 0.3660348960185412\n",
      "排名第 5 名 2013 分数为 0.4311027438487055\n",
      "排名第 6 名 2014 分数为 0.4994684575541035\n",
      "排名第 7 名 2015 分数为 0.643683937721413\n",
      "排名第 8 名 2016 分数为 0.7001496274415703\n",
      "排名第 9 名 2017 分数为 0.7329367430146443\n",
      "排名第 10 名 2018 分数为 0.7854950294904286\n"
     ]
    }
   ],
   "source": [
    "#                     //======================================================================//\n",
    "#                     //                                                                      //\n",
    "#                     //                   Copyright (C) Wang Dong                             //\n",
    "#                     //                   All rights reserved                                 //\n",
    "#                     //                                                                       //\n",
    "#                     //                   filename :熵权TOPSIS                                //\n",
    "#                     //                   description :                                       //\n",
    "#                     //                                                                       //\n",
    "#                     //               created by 王东 at  04/24/2021 10:41:28                 //\n",
    "#                     //                                                                       //\n",
    "#                     // ======================================================================//\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_excel(\"C:/Users/Administrator/Desktop/熵权TOPSIS.xlsx\", sheet_name=0, header=0, index_col=0)\n",
    "m, n = data.shape  # 获取行数m和列数n\n",
    "\n",
    "\n",
    "# 熵权法计算\n",
    "def Y_ij(data1):  # 矩阵标准化(min-max标准化)\n",
    "    for i in data1.columns:\n",
    "        for j in range(n + 1):\n",
    "            if i == str(f'X{j}负'):  # 负向指标\n",
    "                data1[i] = (np.max(data1[i]) - data1[i]) / (np.max(data1[i]) - np.min(data1[i]))\n",
    "            else:  # 正向指标\n",
    "                data1[i] = (data1[i] - np.min(data1[i])) / (np.max(data1[i]) - np.min(data1[i]))\n",
    "    return data1\n",
    "\n",
    "\n",
    "Y_ij = Y_ij(data)  # 标准化矩阵\n",
    "None_ij = [[None] * n for i in range(m)]  # 新建空矩阵\n",
    "print(\"\" * 2)\n",
    "print(\"标准化矩阵如下：\")\n",
    "print(Y_ij)\n",
    "print(\"========================\" * 3)\n",
    "def E_j(data3):  # 计算熵值\n",
    "    data3 = np.array(data3)\n",
    "    E = np.array(None_ij)\n",
    "    for i in range(m):\n",
    "        for j in range(n):\n",
    "            if data3[i][j] == 0:\n",
    "                e_ij = 0.0\n",
    "            else:\n",
    "                P_ij = data3[i][j] / data3.sum(axis=0)[j]  # 计算比重\n",
    "                e_ij = (-1 / np.log(m)) * P_ij * np.log(P_ij)\n",
    "            E[i][j] = e_ij\n",
    "    E_j = E.sum(axis=0)\n",
    "    return E_j\n",
    "\n",
    "\n",
    "E_j = E_j(Y_ij)  # 熵值\n",
    "G_j = 1 - E_j  # 计算差异系数\n",
    "W_j = G_j / sum(G_j)  # 计算权重\n",
    "WW = pd.Series(W_j, index=data.columns, name='指标权重')\n",
    "print(\"各指标的权重为\")\n",
    "print(WW)\n",
    "\n",
    "# TOPSIS计算\n",
    "Y_ij = np.array(Y_ij)  # Y_ij为标准化矩阵\n",
    "Z_ij = np.array(None_ij)  # 空矩阵\n",
    "for i in range(m):\n",
    "    for j in range(n):\n",
    "        Z_ij[i][j] = Y_ij[i][j] * W_j[j]  # 计算加权标准化矩阵Z_ij\n",
    "Imax_j = Z_ij.max(axis=0)  # 最优解\n",
    "Imin_j = Z_ij.min(axis=0)  # 最劣解\n",
    "Dmax_ij = np.array(None_ij)\n",
    "Dmin_ij = np.array(None_ij)\n",
    "for i in range(m):\n",
    "    for j in range(n):\n",
    "        Dmax_ij[i][j] = (Imax_j[j] - Z_ij[i][j]) ** 2\n",
    "        Dmin_ij[i][j] = (Imin_j[j] - Z_ij[i][j]) ** 2\n",
    "Dmax_i = Dmax_ij.sum(axis=1) ** 0.5  # 最优解欧氏距离\n",
    "Dmin_i = Dmin_ij.sum(axis=1) ** 0.5  # 最劣解欧氏距离\n",
    "C_i = Dmin_i / (Dmax_i + Dmin_i)  # 综合评价值\n",
    "Dmax_i = pd.Series(Dmax_i, index=data.index, name='最优解')\n",
    "Dmin_i = pd.Series(Dmin_i, index=data.index, name='最劣解')\n",
    "C_i = pd.Series(C_i, index=data.index, name='综合评价值')\n",
    "print(\"========================\" * 3)\n",
    "print(\"--------最优解-------\")\n",
    "print(Dmax_i)\n",
    "print(\"--------end-------\")\n",
    "print(\"========================\" * 3)\n",
    "print(\"--------最劣解-------\")\n",
    "print(Dmin_i)\n",
    "print(\"--------end-------\")\n",
    "print(\"========================\" * 3)\n",
    "print(\"--------综合评价值-------\")\n",
    "print(C_i)\n",
    "print(\"--------end-------\")\n",
    "print(\"========================\" * 3)\n",
    "print(\"--------综合排名-------\")\n",
    "print(C_i.sort_index(ascending=False))\n",
    "print(\"========================\" * 3)\n",
    "print(\"--------最终排名-------\")\n",
    "X = C_i.sort_index(ascending=True)\n",
    "Y=len(X)\n",
    "# print(\"熵权TOPSIS最终得分第\"+\"名\"+C_i.index[0]+\"它的得分是\"+C_i.values[0])\n",
    "for i in range(1,Y):\n",
    "    print(\"排名第\", end=' ')\n",
    "    print(i, end=' ')\n",
    "    print(\"名\", end=' ')\n",
    "    print(C_i.index[i], end=' ')\n",
    "    print(\"分数为\", end=' ')\n",
    "    print(C_i.values[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
